//
// Created by Administrator on 2025/7/22.
//

#ifndef _FIFO_H
#define _FIFO_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdlib.h>

typedef struct {
    size_t   head;      /** linked list head */
    size_t   tail;      /** linked list tail */
    uint8_t  *buffer;   /** cache buffer pointer */
    size_t   size;      /** cache buffer size */
    uint8_t  full;      /** cache full flags */
    uint32_t overflow;  /** Buffer overflow count */
    uint8_t  pushLock;  /** buffer write lock */
    uint8_t  pullLock;  /** buffer read lock */
} Fifo_t;

void fifo_init(Fifo_t *ll, uint8_t *buffer, size_t size);
void fifo_lock(Fifo_t *ll);
void fifo_unlock(Fifo_t *ll);
void fifo_clear(Fifo_t *ll);
size_t fifo_available(Fifo_t *ll);
size_t fifo_push(Fifo_t *ll, uint8_t *data, size_t len);
size_t fifo_pull(Fifo_t *ll, uint8_t *data, size_t len);

size_t fifo_read_into_keyword(Fifo_t *ll, char key, uint8_t *data, size_t len);
size_t fifo_read_head_to_tail(Fifo_t *ll, char head, char tail, uint8_t *data, size_t len);

#ifdef __cplusplus
}
#endif

#endif //_FIFO_H
